package com.yann.bomb;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;


/**
 * Created by yayun.xia on 2018/11/27
 */
public abstract class BaseActivity extends AppCompatActivity {
    private Toolbar toolbar;
    /***是否显示标题栏*/
    private boolean isshowtitle = true;
    /***获取TAG的activity名称**/
    protected final String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isshowtitle) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }

        View parent = LayoutInflater.from(this).inflate(R.layout.activity_base, null);
        toolbar = parent.findViewById(R.id.toolbar);
        FrameLayout flContainer = parent.findViewById(R.id.fl_container);

        if (intiLayout() != 0) {
            flContainer.addView(LayoutInflater.from(this).inflate(intiLayout(), null));
        }
        //设置布局
        setContentView(parent);
        //初始化控件
        initView();
        //设置数据
        initData();
    }

    /**
     * 设置布局
     *
     * @return
     */
    public abstract int intiLayout();

    /**
     * 初始化布局
     */
    public abstract void initView();

    /**
     * 设置数据
     */
    public abstract void initData();

    /**
     * 是否设置标题栏
     *
     * @return
     */
    public void setTitle(boolean ishow) {
        isshowtitle = ishow;
    }

    public void setTitle(String title){
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    protected <T extends View> T findView(int id) {
        return (T) findViewById(id);
    }

}
